package com.newbee.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.adapter.AskFriendsListViewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.listbean.FriendsAskBean;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.sqlite.DBHelper;

public class RequestFriendsListActivity extends Activity implements
		OnClickListener, OnItemClickListener, OnRefrushActivity
{

	private ListView mListView;
	private DBHelper mDbHelper;
	private List<FriendsAskBean> list;
	private AskFriendsListViewAdapter mAdapter;
	private AlertDialog.Builder friendsbuilder;

	private int mPos;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.activity_ask_friends);
		mDbHelper = new DBHelper(this);
		friendsbuilder = new AlertDialog.Builder(this);
		initLayout();
		initListView();

	}

	private void initListView()
	{

		list = mDbHelper.getFriendAsk();
		mAdapter = new AskFriendsListViewAdapter(this, list);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);

	}

	private void initLayout()
	{
		friendsbuilder.setTitle("好友添加请求");
		friendsbuilder.setMessage("是否接受添加好友邀请？");
		this.mListView = (ListView) this.findViewById(R.id.ask_friend_listview);
		this.findViewById(R.id.ask_friend_back).setOnClickListener(this);

	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.ask_friend_back:
			this.finish();
			break;

		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3)
	{

		TextView statusTextView = (TextView) v
				.findViewById(R.id.ask_friend_status);
		String statueStr = statusTextView.getText().toString();
		if (statueStr != null && statueStr.equals("[未处理]"))
		{

			final FriendsAskBean bean = list.get(p);
			mPos = bean.getFid();
			friendsbuilder.setPositiveButton("接受",
					new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

							Map<String, Object> map = new HashMap<String, Object>();
							map.put(MainServiceKey.ASK_FOR_FRIEND_POCKET_FROM,
									bean.getFromFriend());
							map.put(MainServiceKey.ASK_FOR_FRIEND_POCKET_TO,
									bean.getToFriend());

							MainServiceTask mst = new MainServiceTask(
									TaskType.ASCEPT_FRIEND, map,
									RequestFriendsListActivity.class.getName());
							MainService.addActivityMap(
									RequestFriendsListActivity.class.getName(),
									RequestFriendsListActivity.this);
							MainService.addTaskQueue(mst);
							mDbHelper.upDateFriendsAsk(bean.getFid());

						}
					});
			friendsbuilder.setNegativeButton("拒绝",
					new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

							SmackClient.getInstance().refushAskFriend(
									bean.getFromFriend(), bean.getToFriend());
							Toast.makeText(RequestFriendsListActivity.this,
									"已拒绝加为好友", Toast.LENGTH_LONG).show();
							RequestFriendsListActivity.this.finish();
						}
					});
			friendsbuilder.show();
		}

	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.ASCEPT_FRIEND:
		{
			boolean b = (Boolean) paras[0];
			if (b)
			{
				mDbHelper.upDateFriendsAsk(mPos);
				Toast.makeText(this, "成功接受好友", Toast.LENGTH_LONG).show();
				this.finish();
			}
			else
			{
				Toast.makeText(this, "接受加友申请出现问题！", Toast.LENGTH_LONG).show();
			}
			break;
		}

		default:
			break;
		}
		return null;

	}
}
